import axios from 'axios'
import utilAnt from '@/config/frame/ant/util/util-ant'
import Config from "../Config";

const TOKEN_HEADER = 'ntd_token'

const service = axios.create({
  baseURL: Config.baseURL,
  timeout: 15000
})
service.interceptors.request.use(handleRequest)
service.interceptors.response.use(handleResponse)

function handleRequest(config) {
    const token = window.$Cookie.get(TOKEN_HEADER) || 'fmhb'
    config.headers[TOKEN_HEADER] = 'ntd_' + token
    return config
}

function handleResponse(res) {
    res = res.data
    switch (res.code) {
        case 0:
        case '0':
        case 200:
        case '200': return res.data || res.list || res
        case '500': utilAnt.msgErr(res.msg)
    }
}

export default service

export function get(url, params) {
    return service({
        url, method: 'get', params
    })
}

export function put(url, data) {
    return service({
        url, method: 'put', data
    })
}

